package com.kedacom.freedomLabel.viewmodel;


import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.OnLifecycleEvent;
import android.support.annotation.Nullable;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.model.bean.Category;
import com.kedacom.freedomLabel.model.bean.Item;
import com.kedacom.freedomLabel.model.bean.ItemCategoryName;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.DataCallback;
import com.kedacom.freedomLabel.model.db.DataLoader;
import com.kedacom.freedomLabel.model.db.dao.BusinessDao;
import com.kedacom.freedomLabel.model.db.dao.CategoryDao;
import com.kedacom.freedomLabel.model.db.dao.ItemDao;
import com.kedacom.lego.message.LegoMessageManager;
import com.seray.scaleviewlib.utils.CacheDiskUtils;

import java.util.ArrayList;
import java.util.List;


public class BusinessViewModel extends BaseViewModel {

    public MutableLiveData<List<ItemCategoryName>> mItemCategoryName = new MutableLiveData<>();
    public MutableLiveData<Category> category = new MutableLiveData<>();
    public MutableLiveData<Integer> busicount = new MutableLiveData<>();


    public MutableLiveData<List<Item>> mItems = new MutableLiveData<>();

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void prepareItemColumnNames() {
        String usageCategory = CacheDiskUtils.getInstance(Options.LOCAL_USAGE_CATEGORY_FILE_NAME)
                .getString(Category.class.getSimpleName());
        if (usageCategory == null || usageCategory.isEmpty()) {
            LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_BUSI_NO_CATEGORY);
        } else {
            CategoryDao dao = AppDatabase.getInstance().categoryDao();
            DataLoader.get(dao, usageCategory, new DataCallback<List<Category>>() {
                @Override
                public void success(@Nullable List<Category> data) {
                    if (data != null && !data.isEmpty()) {
                        category.setValue(data.get(0));
                        String[] columnNames = category.getValue().columnNames;
                        List<ItemCategoryName> itemCategoryNames = new ArrayList<>();
                        for (int i = 0; i < columnNames.length; i++) {
                            itemCategoryNames.add(new ItemCategoryName(columnNames[i], new Item()));
                        }
                        mItemCategoryName.setValue(itemCategoryNames);
                    } else {
                        LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_BUSI_NO_CATEGORY);
                    }
                }

                @Override
                public void error(@Nullable String errorMessage) {
                    LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_BUSI_NO_CATEGORY);
                }
            });
        }
    }

    public void getCurCategoryItems(String  parentid,String category){
        ItemDao itemDao = AppDatabase.getInstance().itemDao();
        DataLoader.get(itemDao, parentid, category, new DataCallback<List<Item>>() {
            @Override
            public void success(@Nullable List<Item> data) {
                mItems.setValue(data);
            }

            @Override
            public void error(@Nullable String errorMessage) {

            }
        });
    }

    public void getCount(){
        BusinessDao businessDao = AppDatabase.getInstance().businessDao();
        DataLoader.count(businessDao, new DataCallback<Integer>() {
            @Override
            public void success(@Nullable Integer data) {
                if (data != null) {
                    busicount.setValue(data);
                } else {
                    busicount.setValue(0);
                }
            }

            @Override
            public void error(@Nullable String errorMessage) {
                busicount.setValue(0);
            }
        });
    }

}
